{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=219, shape=(3, 4), dtype=float32, numpy=\n",
       "array([[-0.5083256 , -0.8676372 , -0.04281294, -1.9311084 ],\n",
       "       [ 1.3117619 , -0.08776671,  0.5579821 ,  1.4077363 ],\n",
       "       [ 0.18361318,  0.94494146, -0.31174743,  1.3439546 ]],\n",
       "      dtype=float32)>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "X, W_xh = tf.random.normal(shape=(3, 1)), tf.random.normal(shape=(1, 4))\n",
    "H, W_hh = tf.random.normal(shape=(3, 4)), tf.random.normal(shape=(4, 4))\n",
    "tf.matmul(X, W_xh) + tf.matmul(H, W_hh)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将矩阵X和H按列（维度1）连结，连结后的矩阵形状为(3, 5)。可见，连结后矩阵在维度1的长度为矩阵X和H在维度1的长度之和（ 1+4 ）。然后，将矩阵W_xh和W_hh按行（维度0）连结，连结后的矩阵形状为(5, 4)。最后将两个连结后的矩阵相乘，得到与上面代码输出相同的形状为(3, 4)的矩阵。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: id=226, shape=(3, 4), dtype=float32, numpy=\n",
       "array([[-0.5083256 , -0.8676373 , -0.04281296, -1.9311084 ],\n",
       "       [ 1.3117617 , -0.08776676,  0.5579822 ,  1.4077362 ],\n",
       "       [ 0.18361317,  0.94494146, -0.3117473 ,  1.3439546 ]],\n",
       "      dtype=float32)>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.matmul(tf.concat([X,H],axis=1),tf.concat([W_xh,W_hh],axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
